//########################################################################
//## (C) by Edwin Vriethoff under Microsoft Reciprocal License (Ms-RL)  ##
//## See http://www.codeplex.com/SPUserPoll/license for full license    ##
//##                                                                    ##
//## http://edwin.vriethoff.net http://www.codeplex.com/SPUserPoll/     ##
//########################################################################

using System;
using System.Collections.Specialized;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SPUserPoll.Presentation.FieldTypes
{
    /// <summary>
    /// Altered functionality of the normal SPFieldLookup
    /// </summary>
    class SPUserPollLookup : SPFieldLookup
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="fields"></param>
        /// <param name="fieldName"></param>
        public SPUserPollLookup(
        SPFieldCollection fields,
        string fieldName)
            : base(fields, fieldName)
        {
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="fields"></param>
        /// <param name="typeName"></param>
        /// <param name="displayName"></param>
        public SPUserPollLookup(
        SPFieldCollection fields,
        string typeName,
        string displayName)
            : base(fields, typeName, displayName)
        {
        }

        /// <summary>
        /// Get the answer selector
        /// </summary>
        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                BaseFieldControl ProfileFieldControl = new SPUserPollLookupField(GetPollName());              
                ProfileFieldControl.FieldName = InternalName;
                return ProfileFieldControl;

            }
        }

        /// <summary>
        /// Get the current poll name
        /// </summary>
        /// <returns></returns>
        private string GetPollName()
        {
            string itemFolder = null;

            NameValueCollection queryString = HttpContext.Current.Request.QueryString;

            // if the value wasn't found, try the source url                   
            string itemID = queryString["ID"];

            if (string.IsNullOrEmpty(itemID) == false)
            {
                //get the corresponding listitem
                Data.Components.ResponseList responseList = new SPUserPoll.Data.Components.ResponseList();
                SPListItem listItem = responseList.GetItemByID(Convert.ToInt32(itemID));
                //retrieve the folder name from the itemurl, because the folder property of the item is always empty :(
                itemFolder = GetFolderName(listItem.Url, 2);
            }
            else
            {
                //IF the ID is not available, it is a new item. Retrieve the item from the RootFolder parameter
                string rootFolder = queryString["RootFolder"];
                itemFolder = GetFolderName(rootFolder, 1);
            }

            return itemFolder;
        }


        /// <summary>
        /// Get the foldername of the given url
        /// </summary>
        /// <param name="itemUrl"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        private string GetFolderName(string itemUrl, int position)
        {
            if (itemUrl != null)
            {
                string[] folders = itemUrl.Split("/".ToCharArray());
                if (folders.Length > 0)
                {
                    return (folders[folders.Length - position]);
                }
            }
            //return empty string
            return null;
        }



    }
}